package com.rao.core.excel.parse;


import com.rao.core.excel.entity.StudentExcel;
import com.rao.core.excel.typeHandler.TypeHandler;
import com.rao.core.excel.typeHandler.impl.DateTypeHandler;
import com.rao.core.excel.typeHandler.impl.NumberTypeHandler;
import com.rao.core.excel.typeHandler.impl.StringTypeHandler;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;
import java.util.*;

public class ParseExcelUtil {

    private final static List<TypeHandler> typeHandlers;
    private final static Map<Integer, Field> fieldMap = new HashMap<>();

    static {
        Class<StudentExcel> studentClass = StudentExcel.class;
        Field[] declaredFields = studentClass.getDeclaredFields();

        Set<Integer> number = new HashSet<>();
        Set<Integer> string = new HashSet<>();
        Set<Integer> date = new HashSet<>();

        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            fieldMap.put(i, declaredField);
            Class<?> fieldType = declaredField.getType();
            if (fieldType == String.class) {
                string.add(i);
            } else if (fieldType == Integer.class) {
                number.add(i);
            } else if (fieldType == Date.class) {
                date.add(i);
            }

        }
        typeHandlers = Arrays.asList(new NumberTypeHandler(number), new StringTypeHandler(string), new DateTypeHandler(date));
    }

    public static void parseExcel2Sheet(String excelPath, List<StudentExcel> studentExcels) {
        parseExcel2Sheet(new File(excelPath), studentExcels);
    }

    public static void parseExcel2Sheet(File excelFile, List<StudentExcel> studentExcels) {
        Workbook workbook = null;
        try {
            workbook = new XSSFWorkbook(excelFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int numberOfSheets = workbook.getNumberOfSheets();
        for (int i = 0; i < numberOfSheets; i++) {
            parseSheet2Row(workbook.getSheetAt(i), studentExcels);
        }
    }

    public static void parseSheet2Row(Sheet sheet, List<StudentExcel> studentExcels) {
        if (sheet == null) {
            return;
        }
        System.out.println(Thread.currentThread().getName() + "--->>>>" + "parseSheet2Row");
        int numberOfRows = sheet.getPhysicalNumberOfRows();
        for (int i = ExcelConstants.start_parse_row; i < numberOfRows; i++) {
            row2Object(sheet.getRow(i), studentExcels);
        }
    }

    public static void row2Object(Row row, List<StudentExcel> studentExcels) {
        if (null == row) {
            return;
        }
        int numberOfCells = row.getPhysicalNumberOfCells();
        StudentExcel studentExcel = new StudentExcel();
        for (int i = ExcelConstants.start_parse_column; i < numberOfCells; i++) {
            Cell cell = row.getCell(i);
            /**<? 获取cell的y轴*/
            int columnIndex = cell.getColumnIndex();
            for (TypeHandler typeHandler : typeHandlers) {
                try {
                    if (typeHandler.hand(cell, columnIndex, fieldMap.get(columnIndex), studentExcel)) {
                        break;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        studentExcels.add(studentExcel);
    }

    public static void main(String[] args) {
        List<StudentExcel> studentExcels = new LinkedList<>();
        String excelPath = System.getProperty("user.dir") + File.separator + "src/main/resources/excel/student.xlsx";
        ParseExcelUtil.parseExcel2Sheet(new File(excelPath), studentExcels);
        studentExcels.forEach(System.out::println);
    }
}
